// Copyright 2019 The rhyme_lph Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
// D [author] rhyme_lph
// E [email]  rhymelph@gmail.com
// S [github] https://github.com/rhymelph

import 'package:base_plugin/base_plugin.dart';
import 'package:finance_app/generated/l10n.dart';
import 'package:finance_app/src/common/app_colors.dart';
import 'package:finance_app/src/common/app_spaces.dart';
import 'package:finance_app/src/widgets/button/splash_screen_button.dart';
import 'package:flutter/material.dart';
import 'package:finance_app/src/provider/splash_second_provider.dart';
import 'package:finance_app/src/router_gen/router_meta.dart';
import 'package:flutter_svg/flutter_svg.dart';

@RouterMeta(paramName: "splashSecondPage")
class SplashSecondPage extends StatefulWidget {
  @override
  _SplashSecondPageState createState() => _SplashSecondPageState();
}

class _SplashSecondPageState extends State<SplashSecondPage>
    with PagePresenter<SplashSecondPage, SplashSecondProvider> {
  @override
  Widget get buildWidget => Scaffold(
        backgroundColor: Colors.white,
        body: Container(
            alignment: Alignment.center,
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: [
                Row(
                  mainAxisAlignment: MainAxisAlignment.end,
                  children: [
                    PressedOpacityButton(
                      padding: EdgeInsets.only(
                        right: getDp(47),
                        bottom: getDp(38),
                      ),
                      onPressed: provider.onSkip,
                      child: Text(
                        S.current.skip,
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: getSp(14),
                          fontWeight: FontWeight.w400,
                          height: 28 / 14,
                        ),
                      ),
                    ),
                  ],
                ),
                SizedBox(
                  height: getDp(253),
                  child: PageView.builder(
                    controller: provider.controller,
                    itemBuilder: _buildSplashImage,
                    itemCount: provider.splashImages.length,
                  ),
                ),
                AppSpaces.h30,
                AnimatedBuilder(
                    animation: provider.controller, builder: _buildTitle),
                SizedBox(
                  height: getDp(14),
                ),
                AnimatedBuilder(
                    animation: provider.controller, builder: _buildContent),
                SizedBox(
                  height: getDp(33),
                ),
                AnimatedBuilder(
                    animation: provider.controller, builder: _buildIndicator),
                SizedBox(
                  height: getDp(45),
                ),
                AnimatedBuilder(
                    animation: provider.controller, builder: _buildNextButton),
                SizedBox(
                  height: getDp(30),
                ),
              ],
            )),
      );

  Widget _buildSplashImage(BuildContext context, int index) =>
      SvgPicture.asset(provider.splashImages[index]);

  Widget _buildTitle(BuildContext context, Widget child) {
    int selectIndex = provider.getPageSelectIndex();
    return Text(
      provider.titleList[selectIndex],
      style: TextStyle(
        color: aceBlackColor,
        fontSize: getSp(24),
        fontWeight: FontWeight.w700,
        height: 28 / 24,
      ),
    );
  }

  Widget _buildContent(BuildContext context, Widget child) {
    int selectIndex = provider.getPageSelectIndex();

    return Text(
      provider.contentList[selectIndex],
      style: TextStyle(
        color: aceBlackColor,
        fontSize: getSp(16),
        fontWeight: FontWeight.w400,
        height: 22 / 16,
      ),
      textAlign: TextAlign.center,
    );
  }

  Widget _buildIndicator(BuildContext context, Widget child) {
    int selectIndex = provider.getPageSelectIndex();

    return Row(
      mainAxisSize: MainAxisSize.min,
      children: List.generate(
          provider.splashImages.length,
          (index) => Container(
                margin: EdgeInsets.symmetric(
                  horizontal: getDp(5),
                ),
                width: getDp(9),
                height: getDp(9),
                decoration: BoxDecoration(
                  shape: BoxShape.circle,
                  color: selectIndex == index ? aceBlueColor : aceGrayColor,
                ),
              )),
    );
  }

  Widget _buildNextButton(BuildContext context, Widget child) {
    int selectIndex = provider.getPageSelectIndex();

    return SplashScreenButton(
      onTap: () => provider.onNext(selectIndex),
      title: selectIndex == provider.splashImages.length - 1
          ? S.current.let_s_begin
          : S.current.next,
    );
  }
}
